package com.why.util;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.time.Instant;
import java.time.ZonedDateTime;

public class Result<T> {

    public static final Integer SUCCESSFUL_CODE = 20000;
    public static final String SUCCESSFUL_MESG = "处理成功";

    public static final Integer FAIL_CODE = 40000;
    public static final String FAIL_MESG = "处理失败";

    //处理结果code
    private Integer code;
    //处理结果描述信息
    private String mesg;
    //请求结果生成时间戳
    private Instant time;
    //处理结果数据信息
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public static Integer getSuccessfulCode() {
        return SUCCESSFUL_CODE;
    }

    public static String getSuccessfulMesg() {
        return SUCCESSFUL_MESG;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMesg() {
        return mesg;
    }

    public void setMesg(String mesg) {
        this.mesg = mesg;
    }

    public Instant getTime() {
        return time;
    }

    public void setTime(Instant time) {
        this.time = time;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Result() {
        this.time = ZonedDateTime.now().toInstant();
    }

    /**
     * @param exception
     */


    /**
     * @param exception
     * @param data
     */

    /**
     * 内部使用，用于构造成功的结果
     *
     * @param code
     * @param mesg
     * @param data
     */
    private Result(Integer code, String mesg, T data) {
        this.code = code;
        this.mesg = mesg;
        this.data = data;
        this.time = ZonedDateTime.now().toInstant();
    }

    /**
     * 快速创建成功结果并返回结果数据
     *
     * @param data
     * @return Result
     */
    public static Result success(Object data) {
        return new Result<>(SUCCESSFUL_CODE, SUCCESSFUL_MESG, data);
    }

    /**
     * 快速创建成功结果
     *
     * @return Result
     */
    public static Result success() {
        return success(null);
    }

    /**
     * 系统异常类并返回结果数据
     *
     * @param data
     * @return Result
     */
    public static Result fail(Object data) {
       return new Result<>(FAIL_CODE, FAIL_MESG, data);
    }


    public static Result fail() {
        return new Result<>(FAIL_CODE, FAIL_MESG, null);
    }
//    /**
//     * 系统异常类没有返回数据
//     *
//     * @return Result
//     */
//    public static Result fail() {
//        return new Result(UTownException.SYSTEM_ERROR);
//    }

//    /**
//     * 系统异常类没有返回数据
//     *
//     * @param baseException
//     * @return Result
//     */
//    public static Result fail(BaseException baseException) {
//        return fail(baseException, null);
//    }
//
//    /**
//     * 系统异常类并返回结果数据
//     *
//     * @param data
//     * @return Result
//     */
//    public static Result fail(BaseException baseException, Object data) {
//        return new Result(baseException.getErrorType(), data);
//    }



    /**
     * 成功code=000000
     *
     * @return true/false
     */
    @JsonIgnore
    public boolean isSuccess() {
        return SUCCESSFUL_CODE.equals(this.code);
    }

    /**
     * 失败
     *
     * @return true/false
     */
    @JsonIgnore
    public boolean isFail() {
        return !isSuccess();
    }
}
